//
//  QInvoiceOrderVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/2/22.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QInvoiceOrderVC.h"
#import "QChooseOrderCell.h"
#import "QOrderModel.h"
#import "QInvoiceOrderModel.h"
#import "QChooseOrderDateCell.h"
@interface QInvoiceOrderVC ()<UITableViewDelegate,
UITableViewDataSource>
{
    UIImageView *allImg;//全选图片
    UILabel * courtLab;
    UILabel * priceLab;
}
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)QInvoiceOrderModel * orderModel;
@property (nonatomic,strong)NSMutableArray *selectArr;
@end

@implementation QInvoiceOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title =@"选择开票订单";
    [self createUI];
    [self requestData];
}

- (void)requestData{
    NSString * url =[QParamService urlWithAction:@"findInvoiceOrderList"];
    NSDictionary * para= [QParamService findInvoiceOrderList];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if (error) {
            return ;
        }
        QBaseModel * baseModel =[QBaseModel objectFromDict:dict];
        if([baseModel.result isEqualToString:@"success"]){
            _orderModel = [QInvoiceOrderModel objectFromDict:baseModel.items];
            [_tbView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:baseModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

- (void)createUI{
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo (@0);
        make.left.equalTo (@0);
        make.right.equalTo (@0);
        make.bottom.equalTo (@-70);
    }];
    
    [self.tbView registerNib:[UINib nibWithNibName:@"QChooseOrderCell" bundle:nil] forCellReuseIdentifier:@"QChooseOrderCell"];
    [self.tbView registerNib:[UINib nibWithNibName:@"QChooseOrderDateCell" bundle:nil] forCellReuseIdentifier:@"QChooseOrderDateCell"];
    self.tbView.showsHorizontalScrollIndicator =NO;
    
    //bottomView
    UIView *bottomView =[[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight - 65, kScreenWidth, 65)];
    UIView *line =[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, .7)];
    line.backgroundColor =[QColorUtility defaultLineColor];
    [bottomView addSubview:line];
    [self.view addSubview:bottomView];
    bottomView.backgroundColor =[UIColor whiteColor];
    UIButton *choose =[QViewUtility createButtonWithFrame:CGRectMake(kScreenWidth/2, 0, kScreenWidth/2, 65) font:Ksize17 title:@"确认" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    [choose addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
    [choose whiteStyle];
    [bottomView addSubview:choose];
    
    //全选
    UILabel *lab =[QViewUtility createLabelWithFrame:CGRectMake(kScreenWidth/4 - 30, 7, 60, 20) font:Ksize15 textAlignment:1 textColor:[QColorUtility defaultBackColor] text:@"全选"];
    [bottomView addSubview:lab];
    
    allImg = [UIImageView new];
    [bottomView addSubview:allImg];
    allImg.frame =CGRectMake(kScreenWidth/4 - 45, 9.5, 15, 15);
    allImg.image =[UIImage imageNamed:@"Invoice_unAllChoose"];
    
    
    UILabel *lab1 = [UILabel new];
    [bottomView addSubview:lab1];
    lab1.font =[UIFont systemFontOfSize:13];
    lab1.textAlignment = 1;
    lab1.textColor = [QColorUtility defaultBackColor];
    lab1.text =@"个订单，共";
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(allImg.mas_bottom).offset(8);
        make.height.equalTo (@20);
        make.centerX.equalTo (@(-bottomView.width/4-30));
    }];
    
    courtLab = [UILabel new];
    courtLab.font = [UIFont systemFontOfSize:13];
    [bottomView addSubview:courtLab];
    [courtLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lab1.mas_left).offset(-1);
        make.top.mas_equalTo(allImg.mas_bottom).offset(8);
        make.height.equalTo (@20);
    }];
    courtLab.textColor = [QColorUtility defaultRedColor];
    courtLab.text = @"0";

    
    priceLab =[UILabel new];
    [bottomView addSubview:priceLab];
    priceLab.font = [UIFont systemFontOfSize:13];
    priceLab.textColor = [QColorUtility defaultRedColor];
    priceLab.text = @"0.00";
    [priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lab1.mas_right).offset(0);
        make.top.equalTo(allImg.mas_bottom).offset(8);
        make.height.equalTo (@20);
    }];
    
    UILabel * lab2 =[UILabel new];
    [bottomView addSubview:lab2];
    lab2.text = @"元";
    lab2.textColor = [QColorUtility defaultBackColor];
    lab2.font = [UIFont systemFontOfSize:13];
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(priceLab.mas_right).offset(0);
        make.top.equalTo(allImg.mas_bottom).offset(8);
        make.height.equalTo (@20);
    }];
    
    UITapGestureRecognizer * tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectAllAction)];
    [bottomView addGestureRecognizer:tap];
    
}

- (void)sureAction{
    WS(wSelf);
    if (self.selectArr.count == 0) {

        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"开票订单不能为空" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        return;
    }
    self.OrderBlock(wSelf.selectArr,_orderModel);
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark --- 全选手式
- (void)selectAllAction{
    
    if (_selectArr.count!=_orderModel.list.count) {
        [_selectArr removeAllObjects];
        
        allImg.image = [UIImage imageNamed:@"Invoice_allChoose"];
        [_selectArr addObjectsFromArray:_orderModel.list];
        
    }else{
        [_selectArr removeAllObjects];
        allImg.image = [UIImage imageNamed:@"Invoice_unAllChoose"];

    }
    [self courtAndPrice];
    [_tbView reloadData];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0) {
        return 100;
    }
    QOrderModel *model =_orderModel.list[indexPath.row];
    QOrderModel *model1=_orderModel.list[indexPath.row - 1];
    if (![model1.month isEqualToString:model.month]) {
        return 100;
    }
    
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    //return 44;
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _orderModel.list.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QOrderModel *model =_orderModel.list[indexPath.row];
    
    if (indexPath.row == 0) {
        QChooseOrderDateCell * cell =[tableView dequeueReusableCellWithIdentifier:@"QChooseOrderDateCell"];
        cell.model = model;
        if ([self.selectArr containsObject:model]) {
            cell.selectImg.image = [UIImage imageNamed:@"Invoice_choose.png"];
        }else{
            cell.selectImg.image = [UIImage imageNamed:@"Invoice_unChoose.png"];
        }
        return cell;
    }
    QOrderModel * model1 = _orderModel.list[indexPath.row -1];
    if (![model.month isEqualToString:model1.month]) {
        QChooseOrderDateCell * cell =[tableView dequeueReusableCellWithIdentifier:@"QChooseOrderDateCell"];
        cell.model = model;
        if ([self.selectArr containsObject:model]) {
            cell.selectImg.image = [UIImage imageNamed:@"Invoice_choose.png"];
        }else{
            cell.selectImg.image = [UIImage imageNamed:@"Invoice_unChoose.png"];
        }

        return cell;
    }else{
        QChooseOrderCell * cell= [tableView dequeueReusableCellWithIdentifier:@"QChooseOrderCell" forIndexPath:indexPath];
        cell.model = model;
        if ([self.selectArr containsObject:model]) {
            cell.selectImg.image = [UIImage imageNamed:@"Invoice_choose.png"];
        }else{
            cell.selectImg.image = [UIImage imageNamed:@"Invoice_unChoose.png"];
        }

        return cell;
    }

    

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QOrderModel * model =_orderModel.list[indexPath.row];
    if ([self.selectArr containsObject:model]) {
        [self.selectArr removeObject:model];
    }else{
        [self.selectArr addObject:model];
    }
    [_tbView reloadData];
    if (self.selectArr.count == _orderModel.list.count) {
        allImg.image = [UIImage imageNamed:@"Invoice_allChoose"];
    }else{
        allImg.image = [UIImage imageNamed:@"Invoice_unAllChoose"];
    }
    [self courtAndPrice];
    
}

- (void)courtAndPrice{
    courtLab.text = [NSString stringWithFormat:@"%ld",(unsigned long)self.selectArr.count];
    CGFloat money = 0.0;
    for (QOrderModel * model in self.selectArr) {
        money = [model.amount floatValue] + money;
    }
    priceLab.text = [NSString stringWithFormat:@"%.2f",money];
    
}

#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}
- (NSMutableArray *)selectArr{
    if (!_selectArr) {
        _selectArr = [NSMutableArray array];
    }
    return _selectArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
